package com.sprint.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprint.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class bookDaoTestCase {

    @Autowired
    private BookDao bookDao;
    @Test
    void TestbyId(){
        //System.out.println(bookDao.getbyId(1));
        System.out.println(bookDao.selectById(1));
    }

    @Test
    void testsave(){
        Book book= new Book();
        book.setType("a1");
        book.setName("b2");
        book.setDescription("c3");
        bookDao.insert(book);

    }

    @Test
    void testupdate(){
        Book book= new Book();
        book.setId(2);
        book.setType("33");
        book.setName("33");
        book.setDescription("xxx");
        bookDao.updateById(book);
    }


    @Test
    void testdelete(){
        Book book= new Book();
        book.setId(2);
        book.setType("33");
        book.setName("33");
        book.setDescription("xxx");
        bookDao.deleteById(book);
    }

    @Test
    void testgetAll(){
        //Book book = new Book();
        List<Book> books = bookDao.selectList(null);
        for (Book book:books)
        {
            System.out.println(book.getName());

        }
    }

    @Test
    void testgetPage(){
        IPage page = new Page(2,3);
        bookDao.selectPage(page, null);
        System.out.println("page.getCurrent()="+ page.getCurrent());
        System.out.println("page.getPages()="+page.getPages());
        System.out.println("page.getSize()="+page.getSize());
        System.out.println("page.getTotal()="+page.getTotal());
        System.out.println("page.getRecords()="+page.getRecords());
    }

    @Test
    void testgetByCondition(){
        //select * from tbl_book where name like "%a%"
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.like("name","a");
        bookDao.selectList(qw);
    }

    @Test
    void testgetByCondition2(){
        //select * from tbl_book where name like "%a%"
        String  name="";
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
         qw.like(name!=null,Book::getName,"a");
        //qw.like(String.isNotEmpty(name),Book::getName,"a");
        bookDao.selectList(qw);
    }

}
